package com.example.demoes.service.impl;

import com.example.demoes.service.IndexService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author: rosszhang
 * @date: 2023/12/9 13:18
 * @description:
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class IndexServiceImpl implements IndexService {

    private final RestHighLevelClient restHighLevelClient;

    @Override
    public Object existsIndex(String indexName) throws IOException {
        // 获取索引请求
        GetRequest request = new GetRequest();
        // 设置要查询的索引名称
        request.index(indexName);
        // 执行请求，验证索引是否存在
        boolean isExist = restHighLevelClient.exists(request,RequestOptions.DEFAULT);
        log.info("是否存在：{}", isExist);
        // 根据具体业务逻辑返回不同结果，这里为了方便直接将结果返回
        return isExist;

    }
}
